import vtTouchCom from '../v-drag/vtTouchCom.ts';

const vTmp: any = {};

const VirtualKeyboard = {
    mounted(el: any, binding: any) {
        const { $global } = binding.value?.ujt || {};
        const { canTouch, eStop } = vtTouchCom(el, binding.value);
        if ($global.sys?.onkeyboard && canTouch()) {
            initParams();
            function initParams() {
                if (!$global.keyboard.params) {
                    $global.keyboard.params = {};
                }
            }
            function hideSysKeyboard(dom: any) {
                // 设置输入模式
                dom?.setAttribute('inputmode', 'none');
                // 添加CSS样式
                dom?.style?.setProperty('ime-mode', 'disabled', 'important');
                // vTmp.monacoDomEdit?.addEventListener('blur', handleDown);
                // vTmp.monacoDomEdit?.addEventListener('focus', eStop);
                // vTmp.monacoDomText?.addEventListener('focus', eStop);

                if ($global.keyboard.params.ot) {
                    $global.keyboard.params.ot.fndscb = () => {
                        // // 自动识别inputmode
                        // let inputmode: string = 'text';
                        // if (typeof dom.value === 'number') {
                        //     inputmode = 'numeric';
                        // }
                        // dom?.setAttribute('inputmode', inputmode);

                        // 强制删除inputmode
                        dom?.removeAttribute('inputmode');
                        dom?.style?.setProperty(
                            'ime-mode',
                            'normal',
                            'important'
                        );
                        // vTmp.monacoDomEdit?.removeEventListener(
                        //     'blur',
                        //     handleDown
                        // );
                        // vTmp.monacoDomEdit?.removeEventListener('focus', eStop);
                    };
                }
            }
            function handleDown(e: any) {
                // console.log(666.002, e, e.target?.type);
                $global.keyboard.params?.ot?.fnnumcb?.();
                $global.keyboard.params?.ot?.fndscb?.();
                initParams();
                $global.keyboard.params.ot = {};
                if (e.target?.closest('.noaskeyboard')) {
                    // 不操作什么东西
                } else if (e.target?.closest('.asaimonacoeditor')) {
                    // console.log(666.777, 'asaimonacoeditor');
                    vTmp.monacoDom = e.target.closest('.asaimonacoeditor');
                    vTmp.monacoDomEdit = vTmp.monacoDom.querySelector(
                        '.native-edit-context'
                    );
                    vTmp.monacoDomText =
                        vTmp.monacoDom.querySelector('textarea');
                    hideSysKeyboard(vTmp.monacoDomEdit);
                    if (binding.value?.style) {
                        $global.keyboard.params.ot.style = binding.value.style;
                    }
                    Object.assign($global.keyboard.params, {
                        type: 'monaco',
                        el: vTmp.monacoDomText,
                    });
                    $global.keyboard.isshow = 1;
                } else if (
                    ['textarea', 'text', 'number', 'password'].includes(
                        e.target?.type
                    ) &&
                    !e.target?.disabled
                ) {
                    vTmp.monacoDomEdit = e.target;
                    // console.log(666.123, vTmp.monacoDomEdit);
                    hideSysKeyboard(vTmp.monacoDomEdit);
                    if (vTmp.monacoDomEdit.type === 'number') {
                        $global.keyboard.params.ot.isnumber = true;
                        vTmp.monacoDomEdit.type = 'text';
                        let ream: any = requestAnimationFrame(() => {
                            vTmp.monacoDomEdit.setSelectionRange(
                                vTmp.monacoDomEdit?.value?.length || 0,
                                vTmp.monacoDomEdit?.value?.length || 0
                            );
                            vTmp.monacoDomEdit.focus();
                            cancelAnimationFrame(ream);
                        });
                        $global.keyboard.params.ot.fnnumcb = () => {
                            vTmp.monacoDomEdit.type = 'number';
                        };
                    }
                    if (binding.value?.style) {
                        $global.keyboard.params.ot.style = binding.value.style;
                    }
                    Object.assign($global.keyboard.params, {
                        type: vTmp.monacoDomEdit.type,
                        el: vTmp.monacoDomEdit || el,
                    });
                    $global.keyboard.isshow = 1;
                } else {
                    $global.keyboard.params = {};
                    $global.keyboard.isshow = 0;
                }
            }

            el.addEventListener('pointerdown', handleDown);
            // el.addEventListener('touchstart', eStop);

            vTmp.unmounted = () => {
                el.removeEventListener('pointerdown', handleDown);
                // el.removeEventListener('touchstart', eStop);
                // vTmp.monacoDomText?.removeEventListener('focus', eStop);
            };
        }
    },
    unmounted(el: any) {
        // console.log('unmounted');
        typeof vTmp?.unmounted == 'function' && vTmp.unmounted();
    },
};

export default VirtualKeyboard;
